## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Loading required package: timeDate
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
## 
##     time<-
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks timeSeries::filter(), stats::filter()
## x dplyr::first()  masks xts::first()
## x dplyr::lag()    masks timeSeries::lag(), stats::lag()
## x dplyr::last()   masks xts::last()
## x purrr::reduce() masks rugarch::reduce()
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## 
## Attaching package: 'PerformanceAnalytics'
## The following objects are masked from 'package:timeDate':
## 
##     kurtosis, skewness
## The following object is masked from 'package:graphics':
## 
##     legend
## == Need to Learn tidyquant? ====================================================
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>

Find best ARIMA-GARCH model based on AIC

Note 16/11/2021 - Sometimes ugarchfit() fails to converge (example is AAPL data) for the best ARIMA lags, but converges for lower ARIMA lags. Maybe need to temporarily store the AIC of all ARIMA model, and walk backward the ARIMA model from best to worse AIC if GARCH fails to converge

Load

## `geom_smooth()` using formula 'y ~ x'

## Loading required package: fBasics
## 
## Attaching package: 'fBasics'
## The following object is masked from 'package:TTR':
## 
##     volatility

Seems like AMZN fits SGED better while MSFT fits normal better. Check if we map to the CDF, we get somewhat close to a uniform distribution

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Plot the two in a scatter plot

## `geom_smooth()` using formula 'y ~ x'

Check the correlation between the two stocks and their CDF

## 
## Attaching package: 'psych'
## The following object is masked from 'package:fBasics':
## 
##     tr
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## The following object is masked from 'package:timeSeries':
## 
##     outlier

Fit a Copula to the stocks Restrict to independent, gaussian and t copula Note 16/11/2021 - Study other kinds of copula. When unrestricted, the function tries to fit a “survival BB8” copula. Not sure what that is.

## Bivariate copula: t (par = 0.65, par2 = 5.25, tau = 0.45)

Fit it using copula library, should give the same result as the above

## 
## Attaching package: 'copula'
## The following object is masked from 'package:VineCopula':
## 
##     pobs
## The following object is masked from 'package:lubridate':
## 
##     interval
##     rho.1        df 
## 0.6547093 5.2453013

Create n random samples from the copula, see if they look similar to the AMZN and MSFT data we had

Use the randomly generated CDF returns from the Copula and compare it with the actual return Use the randomly generated standardised residuals to construct a 5% VaR of a portfolio of 50% AMZN and 50% MSFT.

Steps: 1. Using the randomly generated residuals, calculate the 1 step estimate, i.e.

\[ X_{t} = ARIMA + e_t\] \[ e_t = \sigma_t * u_t\] u_t is the randomly generated standardised residuals \[ \sigma_t^2 = GARCH\]

## # A tibble: 2 x 4
##   symbol      exceed_num exceed_prop expected
##   <chr>            <int>       <dbl>    <dbl>
## 1 AMZN_exceed         26      0.0563     23.1
## 2 MSFT_exceed         24      0.0519     23.1

Simple 2 portfolio analysis. Calculate VaR for the portfolio.

## [1] "calculating simple portfolio weight =  0"
## [1] "calculating simple portfolio weight =  0.1"
## [1] "calculating simple portfolio weight =  0.2"
## [1] "calculating simple portfolio weight =  0.3"
## [1] "calculating simple portfolio weight =  0.4"
## [1] "calculating simple portfolio weight =  0.5"
## [1] "calculating simple portfolio weight =  0.6"
## [1] "calculating simple portfolio weight =  0.7"
## [1] "calculating simple portfolio weight =  0.8"
## [1] "calculating simple portfolio weight =  0.9"
## [1] "calculating simple portfolio weight =  1"
## # A tibble: 11 x 5
##    exceed_num exceed_prop expected_exceedance expected_shortfall weight1
##         <int>       <dbl>               <dbl>              <dbl>   <dbl>
##  1         27      0.0584                23.1            -0.0415     0  
##  2         27      0.0584                23.1            -0.0412     0.1
##  3         28      0.0606                23.1            -0.0400     0.2
##  4         29      0.0628                23.1            -0.0392     0.3
##  5         27      0.0584                23.1            -0.0395     0.4
##  6         26      0.0563                23.1            -0.0403     0.5
##  7         24      0.0519                23.1            -0.0381     0.6
##  8         25      0.0541                23.1            -0.0377     0.7
##  9         22      0.0476                23.1            -0.0386     0.8
## 10         21      0.0455                23.1            -0.0372     0.9
## 11         23      0.0498                23.1            -0.0370     1
## [1] "calculating simple portfolio weight =  0"
## [1] "calculating simple portfolio weight =  0.1"
## [1] "calculating simple portfolio weight =  0.2"
## [1] "calculating simple portfolio weight =  0.3"
## [1] "calculating simple portfolio weight =  0.4"
## [1] "calculating simple portfolio weight =  0.5"
## [1] "calculating simple portfolio weight =  0.6"
## [1] "calculating simple portfolio weight =  0.7"
## [1] "calculating simple portfolio weight =  0.8"
## [1] "calculating simple portfolio weight =  0.9"
## [1] "calculating simple portfolio weight =  1"

## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:timeSeries':
## 
##     filter
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

To do: Monte carlo VaR calculation:

VaR calculation based on this paper: https://www.sciencedirect.com/science/article/pii/S0167668709001267 Eqn. 24 to 27

\[ P(X_{p,t} \le VaR_t(\alpha))= \int_{-\infty}^{\infty} \int_{\infty}^{\frac{VaR_t(\alpha)}{2}-x_{2,t}} c(F(x_{1,t}), F(x_{2,t})|\Omega_{t-1})*f(x_{1,t}|\Omega{t-1})*f(x_{2,t}|\Omega{t-1}) dx_{1,t} dx_{2,t} = \alpha \] Where \[ X_{p,t} = wX_{1,t}+(1-w)X_{2,t}\]

Use Monte Carlo Integral to integrate over the VaR